// pages/weekly/weekly.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    submitting: false,
    currentWeek: '2024年第12周',
    weekRange: '3月18日-3月24日',
    projectOptions: [
      { value: '1', label: '生活轨迹系统开发' },
      { value: '2', label: '客户端重构' },
      { value: '3', label: '数据分析平台' },
      { value: '4', label: '小程序开发' }
    ],
    weeklyForm: {
      summary: '',
      projects: [
        {
          id: 1,
          projectId: '',
          projectName: '',
          tasks: [
            { id: 101, content: '' }
          ]
        }
      ],
      nextWeekPlan: '',
      issues: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  onSummaryInput(e) {
    this.setData({
      'weeklyForm.summary': e.detail.value
    })
  },

  onNextWeekPlanInput(e) {
    this.setData({
      'weeklyForm.nextWeekPlan': e.detail.value
    })
  },

  onIssuesInput(e) {
    this.setData({
      'weeklyForm.issues': e.detail.value
    })
  },

  handleProjectChange(e) {
    const index = e.currentTarget.dataset.index
    const projectId = e.detail.value
    const selectedOption = this.data.projectOptions.find(option => option.value === projectId)
    
    this.setData({
      [`weeklyForm.projects[${index}].projectId`]: projectId,
      [`weeklyForm.projects[${index}].projectName`]: selectedOption ? selectedOption.label : ''
    })
  },

  onTaskInput(e) {
    const { projectIndex, taskIndex } = e.currentTarget.dataset
    this.setData({
      [`weeklyForm.projects[${projectIndex}].tasks[${taskIndex}].content`]: e.detail.value
    })
  },

  addProject() {
    const newProject = {
      id: Date.now(),
      projectId: '',
      projectName: '',
      tasks: [
        { id: Date.now() + 1000, content: '' }
      ]
    }
    
    const projects = this.data.weeklyForm.projects
    projects.push(newProject)
    
    this.setData({
      'weeklyForm.projects': projects
    })
  },

  deleteProject(e) {
    const index = e.currentTarget.dataset.index
    const projects = this.data.weeklyForm.projects
    
    if (projects.length <= 1) {
      wx.showToast({
        title: '至少需要保留一个项目',
        icon: 'none'
      })
      return
    }
    
    projects.splice(index, 1)
    
    this.setData({
      'weeklyForm.projects': projects
    })
  },

  addTask(e) {
    const index = e.currentTarget.dataset.index
    const tasks = this.data.weeklyForm.projects[index].tasks
    
    tasks.push({
      id: Date.now(),
      content: ''
    })
    
    this.setData({
      [`weeklyForm.projects[${index}].tasks`]: tasks
    })
  },

  deleteTask(e) {
    const { projectIndex, taskIndex } = e.currentTarget.dataset
    const tasks = this.data.weeklyForm.projects[projectIndex].tasks
    
    if (tasks.length <= 1) {
      wx.showToast({
        title: '至少需要保留一个任务',
        icon: 'none'
      })
      return
    }
    
    tasks.splice(taskIndex, 1)
    
    this.setData({
      [`weeklyForm.projects[${projectIndex}].tasks`]: tasks
    })
  },

  submitWeekly() {
    this.setData({
      submitting: true
    })
    
    setTimeout(() => {
      wx.showToast({
        title: '提交成功',
        icon: 'success'
      })
      
      this.setData({
        submitting: false
      })
    }, 1500)
  }
})